package pl.smath.expression.model;

import junit.framework.TestCase;
import pl.smath.expression.parser.ExpressionParserException;
import pl.smath.expression.parser.text.TextParser;

public class TransformTests extends TestCase {

	private static final String[] TEST_DATA = {
		"a+b",
		"a=3",
		"2*a<=3+b",
		"4/2",
		"x^y",
		"4$2",
		"a*(b+c)",
		"a*b+c",
	};
	private static final String[] TEST_RESULT = {
		"+,a,b",
		"=,a,3",
		"<=,*,2,a,+,3,b",
		"/,4,2",
		"^,x,y",
		"$,4,2",
		"*,a,+,b,c",
		"+,*,a,b,c",
	};

	
	class Transform implements ITransform {
		StringBuilder tranformResult = new StringBuilder();
		
		public int getID() {
			return 0;
		}
		
		public Term process( Term term ) {
			if ( tranformResult.length()!=0 )
				tranformResult.append( ',' );
			tranformResult.append( term.getText() );
			return term;
		}
	}
	
	/**
	 * Testowanie niemodyfikującego visitor'a
	 * @throws ExpressionParserException 
	 */
	public void testData() throws ExpressionParserException {
		Term expression;
		TextParser parser = new TextParser();
		
		for(int i = 0; i < TEST_DATA.length; i++){
			expression = parser.parse( TEST_DATA[i] );
			
			Transform transform = new Transform();
			expression.transform( transform );
			
			assertEquals( "Invalid transformation result of '"+TEST_DATA[i]+"'", TEST_RESULT[i], transform.tranformResult.toString() );
		}
	}
}
